import { Store } from "vuex";
import type { RootState } from "@/store/types";
import { computed, ref } from "vue";
import type { App, Directive } from "vue";
import type { Router } from "vue-router"
interface AuthProp {
  path?: string;
  code: string;
}

function install(app: App, store: Store<RootState>, router: Router) {

  let currentPath = "";
  // router
  router.beforeEach(async (to, from, next) => {
    // 获得当前路径
    currentPath = to.path;
    next();
  });

  const instance: Directive = {
    created(el, binging) {
      const { value } = binging;
      const roles = store.getters['auth/getPermi'];
      // 判断角色信息是否存在并且是数组
      if (value && value instanceof Array) {
        // 当角色存在时
        if (value.length > 0) {
          const permissionRoles = value;
          const hasPermission = roles.some((value) => {
            return permissionRoles.includes(value);
          });
          /**
           * 如果不符合则取出这个dom
           */
          if (!hasPermission) {
            // el.parentNode && el.parentNode.removeChild(el);
            el.style.display='none'
          }
        }
      }
      // let bindingValue = binging.value;
      // function setShowStatus(map: Map<string, string[]>) {

      //   let code = "";
      //   let path = "";

      //   if (typeof bindingValue == "string") {
      //     code = bindingValue;
      //     path = currentPath;
      //   } else {
      //     let p = bindingValue as AuthProp;
      //     path = p.path ?? currentPath;
      //     code = p.code;
      //   }
      //   const v = map.get(path.toLowerCase()) as any[];
      //   if (v && v.find((t) => t.Code == code)) {
      //     if (el.style.display != "") {
      //       el.style.display = "";
      //     }
      //   } else {
      //     if (el.style.display != "none") {
      //       el.style.display = "none";
      //     }
      //   }
      // }





      // // 监听还没有得到按钮的时候
      // store.subscribe((mutations, state) => {

      //   if (mutations.type == "menu/setMenuMap") {

      //     setShowStatus(mutations.payload);

      //   }
      // });
      // // 如果得到了按钮
      // setShowStatus(store.state.menu.menuMap);


    },
  };
  app.directive("permission", instance);
}

export default install;
